package com.yystudy.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yystudy.common.result.Result;
import com.yystudy.model.system.SysPost;
import com.yystudy.model.vo.SysPostQueryVo;
import com.yystudy.system.annotation.Log;
import com.yystudy.system.enums.BusinessType;
import com.yystudy.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author yystudy
 * @since 2023-10-17
 */
@RestController
@RequestMapping("/admin/system/sysPost")
@Api(tags = "岗位管理接口")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;


    /**
     * 岗位分页条件查询
     *
     * @param page
     * @param limit
     * @param sysPostQueryVo
     * @return
     */
    @ApiOperation(value = "岗位分页条件查询")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    public Result list(
            @PathVariable Long page,
            @PathVariable Long limit,
            SysPostQueryVo sysPostQueryVo) {

        //创建page分页对象
        Page<SysPost> pageParam = new Page<>(page, limit);
        IPage<SysPost> pageResult = sysPostService.selectPage(pageParam, sysPostQueryVo);
        return Result.ok(pageResult);
    }


    /**
     * 添加岗位
     *
     * @param sysPost
     * @return
     */
    @Log(title = "岗位管理",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加岗位")
    @PreAuthorize("hasAuthority('bnt.sysPost.add')")
    @PostMapping("/save")
    public Result save(@RequestBody SysPost sysPost) {
        boolean save = sysPostService.save(sysPost);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    public Result getUserById(@PathVariable Long id) {
        SysPost byId = sysPostService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 根据id修改岗位信息
     *
     * @param sysPost
     * @return
     */
    @Log(title = "岗位管理",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改岗位")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @PutMapping("/update")
    public Result update(@RequestBody SysPost sysPost) {
        boolean isSuccess = sysPostService.updateById(sysPost);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 根据id删除岗位
     * @param id
     * @return
     */
    @Log(title = "岗位管理",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除岗位")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean isSuccess = sysPostService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }



    /**
     * 更改岗位状态
     * @param id
     * @param status
     * @return
     */
    @Log(title = "岗位管理",businessType = BusinessType.STATUS)
    @ApiOperation(value = "更改岗位状态")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatusById(@PathVariable Long id,
                                   @PathVariable Integer status){
        sysPostService.updateStatusById(id,status);
        return Result.ok();
    }


}

